#!/usr/bin/env python3
######################################################
#            Linux + Python3 + tkinter
# 提示：
#     该版本不支持添加题目以及离线题库，
#     也不支持在纯 TTY 下运行，
#     但支持了图形界面，并删除了部分无用的依赖库
# 版本：1.1
# 作者：gfdgd xi
######################################################
# 程序信息
min = 1  # 随机数最小值
max = 150  # 随机数最大值
firstnumber = 0  # 默认题目编号
whiletime = 10 # 程序循环次数（即可以在打开一次程序的情况下答几道题）
ti = 0 # 默认分数
tijia = 2 # 答对一道题添加的分数
tijian = 1 # 答错一道题扣的分数
question = "http://gfdgdxi.free.idcfengye.com/random-questions/Question/"  # 题目的下载链接
# 以下内容建议不用随便改动
questionurl = question + "Internet%20Question.txt"
questionnumberurl = question + "Question%27s%20Number.txt"
questionnumber = 0 # 总题目数
nowquestion = 0 # 默认题号
i = 0 # 默认题目数
# 所需库的引入
import random
import os
import tkinter as tk
import tkinter.messagebox
import sys

####################
# 程序对应的事件
####################

def checksysargv(canshu):
    a = sys.argv
    return canshu in a
    
# 使用wget 下载文件
def download(url, path):
    os.system("wget \"" + url + "\"  -O \"" + path + "\"")

# 读取文本文档
def readtxt(path):
    f = open(path,"r") # 设置文件对象
    str = f.read() # 获取内容
    f.close() # 关闭文本对象
    return str # 返回结果


# 用于显示第一个问题的事件
def first_run():
    global ti
    global nowquestion
    global questionnumber
    global i
    # 下载题目学习文件
    print("请稍后，正在获取题目信息，请稍后（请不要按下“Ctrl+C”键，否则会容易出现程序错误的情况）……")
    download(questionurl, "./Question/Internet Question.txt")
    download(questionnumberurl, "./Question/Question Number.txt")

    # 获取题目
    a = readtxt("./Question/Question Number.txt").lstrip().rstrip()
    questionnumber = int(a)  # 获取题目数量
    questionjia = random.randint(min, max)
    nowquestion = firstnumber
    # 加载题号题目
    if questionjia + nowquestion > questionnumber:
        nowquestion = (questionjia + nowquestion) - questionnumber
    else:
        nowquestion = questionjia + nowquestion
    # 下载题目
    print("正在下载对应题目选项，请稍后（请不要随便按下“Ctrl+C”键，不然会出现无答题选项或前一道题答题选项的情况）……")
    questionAurl = question + str(nowquestion) + "/A.txt"
    questionBurl = question + str(nowquestion) + "/B.txt"
    questionCurl = question + str(nowquestion) + "/C.txt"
    questionDurl = question + str(nowquestion) + "/D.txt"
    questionAnalysisurl = question + str(nowquestion) + "/Analysis.txt"
    questionQuestionurl = question + str(nowquestion) + "/Question.txt"
    questionRightChooseurl = question + str(nowquestion) + "/Right%20Choose.txt"
    download(questionAurl, "./Question/Question/A.txt")
    download(questionBurl, "./Question/Question/B.txt")
    download(questionCurl, "./Question/Question/C.txt")
    download(questionDurl, "./Question/Question/D.txt")
    print("正在下载对应题目答案和解析，请稍后（请不要随便按下“Ctrl+C”键，不然会出现无正确答案和解析的情况）……")
    download(questionAnalysisurl, "./Question/Question/Analysis.txt")
    download(questionQuestionurl, "./Question/Question/Question.txt")
    download(questionRightChooseurl, "./Question/Question/Right Choose.txt")
    i += 1
    label1_text.set("第" + str(i) + "题（共计" + str(whiletime) + "题），题号（全部题目数：" + str(questionnumber) + "）：" + str(nowquestion) + "，当前得分：" + str(ti))
    textbox1.config(state=tk.NORMAL)
    textbox1.delete('1.0','end')
    textbox1.insert('end',readtxt("./Question/Question/Question.txt"))
    textbox1.config(state=tk.DISABLED)
    Achoose.set("A." + readtxt("./Question/Question/A.txt"))
    Bchoose.set("B." + readtxt("./Question/Question/B.txt"))
    Cchoose.set("C." + readtxt("./Question/Question/C.txt"))
    Dchoose.set("D." + readtxt("./Question/Question/D.txt"))

# 用于显示下一个问题的事件
def next_question():
    global ti
    global nowquestion
    global questionnumber
    global whiletime
    global i
    questionjia = random.randint(min, max)
    nowquestion = firstnumber
    # 加载题号题目
    if questionjia + nowquestion > questionnumber:
        nowquestion = (questionjia + nowquestion) - questionnumber
    else:
        nowquestion = questionjia + nowquestion
    # 下载题目
    print("正在下载对应题目选项，请稍后（请不要随便按下“Ctrl+C”键，不然会出现无答题选项或前一道题答题选项的情况）……")
    questionAurl = question + str(nowquestion) + "/A.txt"
    questionBurl = question + str(nowquestion) + "/B.txt"
    questionCurl = question + str(nowquestion) + "/C.txt"
    questionDurl = question + str(nowquestion) + "/D.txt"
    questionAnalysisurl = question + str(nowquestion) + "/Analysis.txt"
    questionQuestionurl = question + str(nowquestion) + "/Question.txt"
    questionRightChooseurl = question + str(nowquestion) + "/Right%20Choose.txt"
    download(questionAurl, "./Question/Question/A.txt")
    download(questionBurl, "./Question/Question/B.txt")
    download(questionCurl, "./Question/Question/C.txt")
    download(questionDurl, "./Question/Question/D.txt")
    print("正在下载对应题目答案和解析，请稍后（请不要随便按下“Ctrl+C”键，不然会出现无正确答案和解析的情况）……")
    download(questionAnalysisurl, "./Question/Question/Analysis.txt")
    download(questionQuestionurl, "./Question/Question/Question.txt")
    download(questionRightChooseurl, "./Question/Question/Right Choose.txt")
    if i >= whiletime:
        label1_text.set("第" + str(i) + "题（共计" + str(whiletime) + "题），题号（全部题目数：" + str(questionnumber) + "）：" + str(nowquestion) + "，当前得分：" + str(ti))
        label2_text.set("如需继续答题，请重新启动该程序以便答题！")
        label1.config(state=tk.DISABLED)
        label2.config(state=tk.DISABLED)
        textbox1.config(state=tk.DISABLED)
        textbox2.config(state=tk.DISABLED)
        button1.config(state=tk.DISABLED)
        button2.config(state=tk.DISABLED)
        radiobutton1.config(state=tk.DISABLED)
        radiobutton2.config(state=tk.DISABLED)
        radiobutton3.config(state=tk.DISABLED)
        radiobutton4.config(state=tk.DISABLED)
    else:
        i += 1
        label1_text.set("第" + str(i) + "题（共计" + str(whiletime) + "题），题号（全部题目数：" + str(questionnumber) + "）：" + str(nowquestion) + "，当前得分：" + str(ti))
        label2_text.set("")
        textbox1.config(state=tk.NORMAL)
        textbox1.delete('1.0', 'end')
        textbox1.insert('end', readtxt("./Question/Question/Question.txt"))
        textbox1.config(state=tk.DISABLED)
        textbox2.config(state=tk.NORMAL)
        textbox2.delete('1.0', 'end')
        textbox2.config(state=tk.DISABLED)
        Achoose.set("A." + readtxt("./Question/Question/A.txt"))
        Bchoose.set("B." + readtxt("./Question/Question/B.txt"))
        Cchoose.set("C." + readtxt("./Question/Question/C.txt"))
        Dchoose.set("D." + readtxt("./Question/Question/D.txt"))
        button1.config(state=tk.NORMAL)
        button2.config(state=tk.DISABLED)

# 检查问题答案
def check_answer():
    global ti
    global nowquestion
    global questionnumber
    daan = readtxt("./Question/Question/Right Choose.txt")  # .encode('gb18030')
    daanstr = ""
    if daan == "1":
        daanstr = "A"
    if daan == "2":
        daanstr = "B"
    if daan == "3":
        daanstr = "C"
    if daan == "4":
        daanstr = "D"
    if daan == str(check.get()):
        ti += tijia
        label2_text.set("回答正确，正确答案为" + daanstr)
    else:
        ti -= tijian
        label2_text.set("回答错误，正确答案为：" + daanstr)
    label1_text.set("第" + str(i) + "题（共计" + str(whiletime) + "题），题号（全部题目数：" + str(questionnumber) + "）：" + str(nowquestion) + "，当前得分：" + str(ti))
    textbox2.config(state=tk.NORMAL)
    textbox2.delete('1.0', 'end')
    textbox2.insert('end', readtxt("./Question/Question/Analysis.txt"))
    textbox2.config(state=tk.DISABLED)
    button2.config(state=tk.NORMAL)
    button1.config(state=tk.DISABLED)

# 显示“关于这个程序”窗口
def about_this_program():
    tkinter.messagebox.showinfo(title="关于这个程序",message="一个基于 Python3 的 tkinter 制作的随机出题器\n目前支持随机出题\n版本：1.1\n适用平台：Linux")

# 显示“提示”窗口
def helps():
    tkinter.messagebox.showinfo(title="提示", message="提示：\n无")

##############
# 检测参数
##############
if checksysargv("--help") or checksysargv("-h"):
    print("帮助：")
    print("    -h --help                            显示程序帮助信息")
    print("    -v --version                         显示程序版本号")
    print("    -a --about                           关于这个程序")
    sys.exit(0) # 退出程序
if checksysargv("--about") or checksysargv("-a"):
    print("关于：")
    print("    程序版本：1.0")
    print("    建议 Python 运行版本：Python 3.x")
    print("    程序所需组件（程序）")
    print("        wget——下载题目文件")
    print("        Python3——程序运行所需的必要条件，程序将运行在这个程序上")
    print("        pip——用于安装程序所需的Python库")
    print("    源码下载地址：https://gitee.com/gfdgd-xi/random-questions-for-linux-python")
    sys.exit(0) # 退出程序
if checksysargv("--version") or checksysargv("-v"):
    print("1.0")
    sys.exit(0) # 退出程序

####################
# 窗体创建
####################

# 创建窗口
window = tk.Tk()
window.title("随机出题")
label1_text = tk.StringVar()
label1 = tk.Label(textvariable=label1_text)
textbox1 = tk.Text(window,show=None,width=80,height=6)
textbox1.config(state=tk.DISABLED)
check = tk.IntVar()
check.set(1)
Achoose = tk.StringVar()
Bchoose = tk.StringVar()
Cchoose = tk.StringVar()
Dchoose = tk.StringVar()
label2_text = tk.StringVar()
radiobutton1 = tk.Radiobutton(window,textvariable=Achoose,value=1,variable=check)
radiobutton2 = tk.Radiobutton(window,textvariable=Bchoose,value=2,variable=check)
radiobutton3 = tk.Radiobutton(window,textvariable=Cchoose,value=3,variable=check)
radiobutton4 = tk.Radiobutton(window,textvariable=Dchoose,value=4,variable=check)
button1 = tk.Button(window,text="确定",command=check_answer)
button2 = tk.Button(window,text="下一道题",command=next_question)
button2.config(state=tk.DISABLED)
label2 = tk.Label(window,textvariable=label2_text)
textbox2 = tk.Text(window,show=None,width=80,height=6)
textbox2.config(state=tk.DISABLED)
menu = tk.Menu(window) # 设置菜单栏
programmenu = tk.Menu(menu,tearoff=0) # 设置“程序”菜单栏
menu.add_cascade(label="程序",menu=programmenu)
programmenu.add_command(label="退出程序",command=window.quit) # 设置“退出程序”项
help = tk.Menu(menu,tearoff=0) # 设置“帮助”菜单栏
menu.add_cascade(label="帮助",menu=help)
help.add_command(label="小提示",command=helps) # 设置“小提示”项
help.add_separator() # 设置分界线
help.add_command(label="关于这个程序",command=about_this_program) # 设置“关于这个程序”项
window.config(menu=menu) # 显示菜单栏
label1.pack()
textbox1.pack()
radiobutton1.pack()
radiobutton2.pack()
radiobutton3.pack()
radiobutton4.pack()
button1.pack()
label2.pack()
textbox2.pack()
button2.pack()
first_run() # 执行运行前事件
window.mainloop()
quit(0) # 防止以终端模式启动

####################
# 终端模式
####################
'''

'''
